package com.ruoyi.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.test.domain.TStoreInfo;
import com.ruoyi.test.domain.dto.TStoreInfoDTO;
import com.ruoyi.test.domain.vo.TStoreInfoVO;
import com.ruoyi.test.service.ITStoreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 门店Controller
 *
 * @author Tellsea
 * @date 2023-08-22
 */
@Slf4j
@Api(tags = "门店")
@RestController
@RequestMapping("/test/storeInfo")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TStoreInfoController extends BaseController {

    private final ITStoreInfoService tStoreInfoService;

    @ApiOperation("查询门店列表")
    @PreAuthorize("@ss.hasPermi('test:storeInfo:list')")
    @GetMapping("/list")
    public R<IPage<TStoreInfoVO>> list(TStoreInfoDTO tStoreInfoDto) {
        IPage<TStoreInfoVO> iPage = tStoreInfoService.queryList(tStoreInfoDto);
        return R.data(iPage,"查询成功");
    }

    @ApiOperation("导出门店列表")
    @PreAuthorize("@ss.hasPermi('test:storeInfo:export')")
    @Log(title = "门店", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TStoreInfoDTO tStoreInfoDto) {
        List<TStoreInfoVO> list = tStoreInfoService.queryAll(tStoreInfoDto);
        ExcelUtil<TStoreInfoVO> util = new ExcelUtil<>(TStoreInfoVO.class);
        util.exportExcel(response, list, "门店数据");
    }

    @ApiOperation(value = "获取门店详细信息",notes = "参数:id 类型:Long")
    @PreAuthorize("@ss.hasPermi('test:storeInfo:query')")
    @GetMapping(value = "/getInfo/{id}")
    public R<TStoreInfo> getInfo(@PathVariable("id") Long id) {
        return R.data(tStoreInfoService.queryById(id),"查询成功");
    }

    @ApiOperation("新增门店")
    @PreAuthorize("@ss.hasPermi('test:storeInfo:add')")
    @RepeatSubmit(interval = 2000)
    @Log(title = "门店", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public R<Object> add(@RequestBody TStoreInfo tStoreInfo) {
        return tStoreInfoService.save(tStoreInfo) ? R.ok("新增成功") : R.fail("新增失败");
    }

    @ApiOperation("修改门店")
    @PreAuthorize("@ss.hasPermi('test:storeInfo:edit')")
    @Log(title = "门店", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public R<Object> edit(@RequestBody TStoreInfo tStoreInfo) {
        return tStoreInfoService.updateById(tStoreInfo) ? R.ok("修改成功") : R.fail("修改失败");
    }

    @ApiOperation(value = "删除门店",notes = "参数: ids 类型: array")
    @PreAuthorize("@ss.hasPermi('test:storeInfo:remove')")
    @Log(title = "门店", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public R<Object> remove(@PathVariable Long[] ids) {
        return tStoreInfoService.removeByIds(Arrays.asList(ids)) ? R.ok("删除成功") : R.fail("删除失败");
    }

}
